home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / General / MM3MCp.sea Folder / Made by Marksman / Sources / mm / mmMainMM_Demo.cp < prev    next >
Encoding:
Text File  |  1994-01-16  |  33.5 KB  |  993 lines  |  [TEXT/MMCC]

  1. /* mmMainMM_Demo */
  2.  
  3. /*    
  4.     Program name:  mmMainMM_Demo  
  5.     Function:  This is the main module for this program.  
  6.     History: 1/16/94 Original by George Cossey
  7.  
  8.    */
  9.  
  10. #include "mmCommonMM_Demo.h"            /* Common */
  11. #include "CommonMM_Demo.h"                /* Common */
  12.  
  13. #include "Aliases.h"                            /* Aliases*/
  14.  
  15.  
  16. Boolean        DoIt;                                /* Flag saying an event is ready */
  17. short        code;                                /* Determine event type */
  18. WindowPtr    whichWindow;                        /* See which window for event */
  19. long        mResult;                            /* Menu list and item selected values */
  20. short        theMenu,theItem;                    /* Menu list and item selected */
  21. Boolean        Is_A_Dialog;                        /* Flag for modless dialogs */
  22. short        charCode, itemHit;                    /* For modeless dialogs*/
  23. char        ch;                                    /* Key pressed in Ascii */
  24. Boolean        DoTheModelessEvent, CmdDown;        /* For modeless dialogs*/
  25. WindowPeek    thePeeked;                            /* For modeless dialogs*/
  26.  
  27.  
  28. /* MAIN entry point */
  29. void main(void);
  30.  
  31. /* ======================================================= */
  32. /* ======================================================= */
  33.  
  34. void CmmApplication::OpenOrPrintDroppedOnFiles()
  35. {
  36. short        doWhat,fileCnt;
  37. AppFile        fileStuff;
  38. OSErr        ErrorCode;
  39.  
  40.  
  41. CountAppFiles(&doWhat,&fileCnt);        /* See if launched by a document */
  42.  
  43. if (fileCnt != 0)                        /* See if open a document */
  44.     {
  45.     GetAppFiles(1,&fileStuff);
  46.     gFiles->Files.Reply.vRefNum = fileStuff.vRefNum;
  47.     PStrCopy((Str255 *)&fileStuff.fName,(Str255 *)&gFiles->Files.inputFileName);
  48.     
  49.     ErrorCode = FSOpen(gFiles->Files.inputFileName,gFiles->Files.Reply.vRefNum,&gFiles->Files.inputRefNum);/* Open the file */
  50.  
  51.     /* NOTE: This code only looks at the first file and assumes it wants to be opened */
  52.     /* Do the work on opening this file for the user */
  53.     /* This could be a print, if so the set doneFlag to true on the way out */
  54.  
  55.     gFiles->Close_The_Input_File();        /* Now close this file */
  56.  
  57.     ClrAppFiles(1);
  58.     }
  59.  
  60. }
  61.  
  62. /* ======================================================= */
  63.  
  64. /* Routine: WNEIsImplemented */
  65. /* Purpose: See if the MultiFinder trap, WaitNextEvent, is available */
  66.  
  67. Boolean CmmApplication::IsWNEIsImplemented()    /* See if WaitNextEvent is available */
  68. {
  69. #define    WNETrapNumber    0xA860                    /* The expected trap number */
  70. #define    kGestaltTrapID    0xA1AD                    /* The expected trap number */
  71.  
  72. SysEnvRec    theWorld;                            /* Environment record */
  73. OSErr        discardError;                        /* Error code returned */
  74. Boolean        theWNEIsImplemented;                /* Value to return */
  75. long        result;                                /* Value returned */
  76.  
  77.  
  78. Black_ForeColor.red = 0x0000;  Black_ForeColor.green = 0x0000;  Black_ForeColor.blue = 0x0000;  /* Get black color */
  79. White_BackColor.red = 0xFFFF;  White_BackColor.green = 0xFFFF;  White_BackColor.blue = 0xFFFF;  /* Get white color */
  80.  
  81. Has.ColorQD = FALSE;                                /* Init to no color QuickDraw */
  82. Has.FPU = FALSE;                                    /* Init to no floating point chip */
  83. Has.AppleEvents = FALSE;                            /* Whether AppleEvents are available */
  84. Has.AliasMgr = FALSE;                                /* Whether AliasMgr is available */
  85. Has.EditionMgr = FALSE;                                /* Whether EditionMgr is available */
  86. Has.Gestalt = FALSE;                                /* Whether Gestalt is available */
  87. Has.NewStdFile = FALSE;                                /* Whether NewStdFile is available */
  88. Has.PPCToolbox = FALSE;                                /* Whether PPCToolbox is available */
  89. Has.QuickDraw32Bit = FALSE;                            /* Whether 32Bit QuickDraw is available */
  90. InTheForeground = TRUE;                                /* Init to a foreground app */
  91.  
  92. discardError = SysEnvirons(1, &theWorld);            /* Check how old this system is */
  93. if (theWorld.machineType < 0)                        /* Negative means really old */
  94.     {
  95.     theWNEIsImplemented = FALSE;                    /* Really old ROMs, no WNE possible */
  96.     }
  97. else
  98.     {
  99.     theWNEIsImplemented = CheckTrapAvailable(WNETrapNumber, ToolTrap);/* See if trap is there */
  100.     Has.ColorQD = theWorld.hasColorQD;                /* Flag for Color QuickDraw being available */
  101.     Has.FPU = theWorld.hasFPU;                        /* Flag for Floating Point Math Chip being available */
  102.     Has.Gestalt = CheckTrapAvailable(kGestaltTrapID, ToolTrap);/* Whether Gestalt is available */
  103.     if (Has.Gestalt)                                /* Do if Gestalt is available */
  104.         {
  105.         discardError = Gestalt(gestaltAliasMgrAttr,&result);
  106.         if ((discardError == 0) && ((result & (0x00000001 << gestaltAliasMgrPresent)) != 0))
  107.             Has.AliasMgr = TRUE;
  108.  
  109.         discardError = Gestalt(gestaltEditionMgrAttr,&result);
  110.         if ((discardError == 0) && ((result & (0x00000001 << gestaltEditionMgrPresent)) != 0))
  111.             Has.EditionMgr = TRUE;
  112.  
  113.         discardError = Gestalt(gestaltAppleEventsAttr,&result);
  114.         if ((discardError == 0) && ((result & (0x00000001 << gestaltAppleEventsPresent)) != 0))
  115.             Has.AppleEvents = TRUE;
  116.  
  117.         discardError = Gestalt(gestaltPPCToolboxAttr,&result);
  118.         if ((discardError == 0) && ((result & (0x00000001 << gestaltPPCToolboxPresent)) != 0))
  119.             Has.PPCToolbox = TRUE;
  120.  
  121.         discardError = Gestalt(gestaltQuickdrawVersion,&result);
  122.         if ((discardError == 0) && ((result & gestalt32BitQD) != 0))
  123.             Has.QuickDraw32Bit = TRUE;
  124.  
  125.         discardError = Gestalt(gestaltStandardFileAttr,&result);
  126.         if ((discardError == 0) && ((result & (0x00000001 << gestaltStandardFile58)) != 0))
  127.             Has.NewStdFile = TRUE;
  128.         }
  129.     }
  130.     
  131. return(theWNEIsImplemented);
  132. }
  133.  
  134. /* ======================================================= */
  135.  
  136. /* Routine: DoOSEvent */
  137. /* Purpose: Handle DoOSEvents */
  138.  
  139. void CmmApplication::DoOSEvent(EventRecord *myEvent)
  140. {
  141.  
  142.  
  143. if (((myEvent->message & osEvtMessageMask) >> 24) == suspendResumeMessage)    /*  See which  */
  144.     {
  145.     if ((myEvent->message & resumeFlag) == 0)    /* Suspend */
  146.         InTheForeground = FALSE;
  147.     else
  148.         InTheForeground = TRUE;
  149.     }
  150. }
  151.  
  152. /* ======================================================= */
  153.  
  154. void CmmApplication::Handle_Extra_User_Event(UserEventPRec theUserEvent)    /* Hook, to handle user events */
  155. {
  156. /* Expected to be overridden by the user code */
  157. }
  158.  
  159. /* ======================================================= */
  160.  
  161. /* Routine: Handle_User_Event */
  162. /* Purpose: Check for user events */
  163.  
  164. void CmmApplication::Handle_User_Event()        /* Check for user events */
  165. {
  166. UserEventRec    TheUserEvent;                    /* The user event */
  167.  
  168.  
  169. GetUserEvent(&TheUserEvent);                    /* Check for any user events */
  170. if (TheUserEvent.ID != UserEvent_None)            /* Only do if we have any */
  171.     {
  172.  
  173.     switch (TheUserEvent.ID)                    /* Key off the Event ID */
  174.         {
  175.         case UserEvent_Open_Window:             /* Open a Window or Modeless dialog */
  176.             switch (TheUserEvent.ID2)            /* Do the appropiate window */
  177.                 {
  178.                 case ResA_My_Alert:
  179.                     gMy_Alert->BringUpAlert();    /* Open this alert */
  180.                     break;
  181.                 case ResD_My_Modal:
  182.                     gMy_Modal->BringUpDialog();    /* Open this modal dialog */
  183.                     break;
  184.                 case ResD_My_Movable_Moda:
  185.                     gMy_Movable_Moda->Open();        /* Open this modeless dialog */
  186.                     break;
  187.                 case ResD_My_Modeless:
  188.                     gMy_Modeless->Open();        /* Open this modeless dialog */
  189.                     break;
  190.                 case ResD_About_Demo:
  191.                     gAbout_Demo->Open();        /* Open this modeless dialog */
  192.                     break;
  193.                 case ResW_My_basic_window: 
  194.                     gMy_basic_window->Open();        /* Open this window */
  195.                     break;
  196.                 case ResW_Floating_window: 
  197.                     gFloating_window->Open();        /* Open this window */
  198.                     break;
  199.                 default:                        /* Handle others */
  200.                     break;
  201.                 }                                /* End of the switch */
  202.             break;
  203.  
  204.         case UserEvent_Close_Window:             /* Close a Window or Modeless dialog */
  205.             switch (TheUserEvent.ID2) {            /* Do the appropiate window */
  206.                 case ResD_My_Movable_Moda:
  207.                     gMy_Movable_Moda->Close(gMy_Movable_Moda->theWindow);/* Close this modeless dialog */
  208.                     break;
  209.                 case ResD_My_Modeless:
  210.                     gMy_Modeless->Close(gMy_Modeless->theWindow);/* Close this modeless dialog */
  211.                     break;
  212.                 case ResD_About_Demo:
  213.                     gAbout_Demo->Close(gAbout_Demo->theWindow);/* Close this modeless dialog */
  214.                     break;
  215.                 case ResW_My_basic_window: 
  216.                     gMy_basic_window->Close((WindowPtr)-1);    /* Close this window */
  217.                     break;
  218.                 case ResW_Floating_window: 
  219.                     gFloating_window->Close((WindowPtr)-1);    /* Close this window */
  220.                     break;
  221.                 default:                        /* Handle others */
  222.                     break;
  223.                 }                                /* End of the switch */
  224.             break;
  225.  
  226.         default:                                /* Not standard, must be program specific */
  227.             break;
  228.         }                                        /* End of switch */
  229.     }
  230. }
  231.  
  232. /* ======================================================= */
  233.  
  234. /* Routine: DoKeyEvent */
  235. /* Purpose: Handle a key pressed */
  236.  
  237. void CmmApplication::DoKeyEvent(EventRecord *myEvent)        /* Handle key presses */
  238. {
  239. short    charCode;                                    /* Key code */
  240. char    ch;                                            /* Key pressed in Ascii */
  241. long    mResult;                                    /* Menu list and item, if a command key */
  242. short    theMenu,theItem;                            /* Menu list and item, if command key */
  243.  
  244.  
  245. charCode = myEvent->message & charCodeMask;        /* Get the character */
  246. ch = (char)charCode;                            /* Change it to ASCII */
  247.  
  248. if ((myEvent->modifiers & cmdKey) != 0)            /* See if Command key is down */
  249.     {
  250.     gMenus->EnableTheMenus();                    /* Let us in to enable and disable menus*/
  251.     mResult = MenuKey(ch);                        /* See if a menu selection */
  252.     theMenu = HiWord(mResult);                    /* Get the menu list number */
  253.     theItem = LoWord(mResult);                    /* Get the menu item number */
  254.     if (theMenu != 0)                            /* See if a list was selected */
  255.         gMenus->HandleMenuSelection(theMenu,theItem);    /* Do the menu selection */
  256.  
  257.     if (((ch == 'x') || (ch == 'X')) && (theInput != NIL))
  258.         TECut(theInput);                        /* Handle a Cut in a TE area */
  259.     if (((ch == 'c') || (ch == 'C')) && (theInput != NIL))
  260.         TECopy(theInput);                        /* Handle a Copy in a TE area */
  261.     if (((ch == 'v')  ||  (ch == 'V')) && (theInput != NIL))
  262.         TEPaste(theInput);                        /* Handle a Paste in a TE area */
  263.     }
  264. else if (theInput != NIL)
  265.     TEKey(ch,theInput);                            /* Place the normal key stroke */
  266. }
  267.  
  268. /* ======================================================= */
  269.  
  270. /* Routine: DoDiskEvent */
  271. /* Purpose: Handle a diskette inserted */
  272.  
  273. void CmmApplication::DoDiskEvent(EventRecord *myEvent)                            /* Handle disk inserted */
  274. {
  275. short    theError;                                    /* Error returned from mount */
  276.  
  277.  
  278. if (HiWord(myEvent->message) != noErr)                /* See if a diskette mount error */
  279.     {
  280.     myEvent->where.h = ((qd.screenBits.bounds.right - qd.screenBits.bounds.left) / 2) - (304 / 2);/* Center horz */
  281.     myEvent->where.v = ((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) / 3) - (104 / 2);/* Top 3ed vertically */
  282.     InitCursor();                                    /* Make sure it has an arrow cursor */
  283.     theError = DIBadMount(myEvent->where, myEvent->message);/* Let the OS handle the diskette */
  284.     }
  285. }
  286.  
  287. /* ======================================================= */
  288.  
  289. /* Routine: DoZoom */
  290. /* Purpose: Handle a window zoom */
  291.  
  292. Boolean CmmApplication::DoZoom(WindowPtr whichWindow,short code,EventRecord *myEvent)/* Handle a window zoom */
  293. {
  294. Rect    OldRect;                                    /* Window rect before the zoom */
  295. Point    myPt;                                        /* Point for tracking zoom box */
  296. long    theRefCon;                                    /* Refcon with layer masked off */
  297. Boolean        HandledEvent;
  298.  
  299.  
  300. HandledEvent = false;
  301. if (!Doing_MovableModal)                            /* Select proper window */
  302.     {
  303.     if (whichWindow != NIL)                            /* See if we have a legal window */
  304.         {
  305.         SetPort(whichWindow);                        /* Get ready to draw in this window */
  306.  
  307.         myPt = myEvent->where;                        /* Get mouse position */
  308.         GlobalToLocal(&myPt);                        /* Make it relative */
  309.  
  310.         OldRect = whichWindow->portRect;            /* Save the rect before resizing */
  311.  
  312.         if (TrackBox(whichWindow, myPt, code))        /* Zoom it */
  313.             {
  314.             ZoomWindow(whichWindow, code, TRUE);    /* Resize to result */
  315.             EraseRect(&whichWindow->portRect);        /* Make sure we update the whole window effectively */
  316.             InvalRect(&whichWindow->portRect);        /* Tell ourselves to update, redraw, the window contents */
  317.             theRefCon = GetWRefCon(whichWindow);    /* Get the refcon */
  318.             theRefCon = theRefCon & 0x00FFFFFF;        /* Mask the layer out */
  319.             HandledEvent = true;
  320.             switch (theRefCon)                        /* Do the appropiate window */
  321.                 {
  322.                 case ResW_My_basic_window:
  323.                     gMy_basic_window->Resized(&OldRect, whichWindow);    /* Resized this window */
  324.                     break;
  325.  
  326.                 default:                            /* Handle others */
  327.                     HandledEvent = false;
  328.                     break;
  329.                 }
  330.             Mk_HiliteWindow(whichWindow);            /* Hilite it again */
  331.             }
  332.         }
  333.     }
  334. return(HandledEvent);
  335. }
  336.  
  337. /* ======================================================= */
  338.  
  339. /* Routine: DoGrow */
  340. /* Purpose: Handle a window resize */
  341.  
  342. Boolean CmmApplication::DoGrow(WindowPtr whichWindow,EventRecord *myEvent)
  343. {
  344. Rect    OldRect;                                    /* Window rect before the grow */
  345. Point    myPt;                                        /* Point for tracking grow box */
  346. Rect    GrowRect;                                    /* Set the grow bounds */
  347. long    mResult;                                    /* Result from the grow */
  348. long    theRefCon;                                    /* Refcon with layer masked off */
  349. Boolean        HandledEvent;
  350.  
  351.  
  352. HandledEvent = false;
  353. if (!Doing_MovableModal)                            /* Select proper window */
  354.     {
  355.     if (whichWindow != nil)                            /* See if we have a legal window */
  356.         {
  357.         SetPort(whichWindow);                        /* Get ready to draw in this window */
  358.  
  359.         myPt = myEvent->where;                        /* Get mouse position */
  360.         GlobalToLocal(&myPt);                        /* Make it relative */
  361.  
  362.         OldRect = whichWindow->portRect;            /* Save the rect before resizing */
  363.  
  364.         SetRect(&GrowRect, 4, 4, (qd.screenBits.bounds.right - qd.screenBits.bounds.left)-4,  (qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - 4);/* l,t,r,b */
  365.         mResult = GrowWindow(whichWindow, myEvent->where, &GrowRect);/* Grow it */
  366.         SizeWindow(whichWindow, LoWord(mResult), HiWord(mResult), true);/* Resize to result */
  367.  
  368.         HandledEvent = true;
  369.         theRefCon = GetWRefCon(whichWindow);        /* Get the refcon */
  370.         theRefCon = theRefCon & 0x00FFFFFF;            /* Mask the layer out */
  371.  
  372.         switch (theRefCon)                            /* Do the appropiate window */
  373.             {
  374.  
  375.             default:                                /* allow other buttons, trap for debug */
  376.                 HandledEvent = false;
  377.                 break;                                /* end of otherwise */
  378.             }                                        /* end of switch */
  379.  
  380.         SetPort(whichWindow);                        /* Get ready to draw in this window */
  381.  
  382.         myPt.h = whichWindow->portRect.right - whichWindow->portRect.left;/* Local right edge */
  383.         myPt.v = whichWindow->portRect.bottom - whichWindow->portRect.top;/* Local bottom edge */
  384.  
  385.         SetRect(&GrowRect, 0, OldRect.bottom - 15, OldRect.right + 15, OldRect.bottom + 15);/* Position for horz scrollbar area */
  386.         EraseRect(&GrowRect);                        /* Erase old area */
  387.         InvalRect(&GrowRect);                        /* Flag us to update it */
  388.  
  389.         SetRect(&GrowRect, OldRect.right - 15, 0, OldRect.right + 15, OldRect.bottom + 15);/* Position for vert scrollbar area */
  390.         EraseRect(&GrowRect);                        /* Erase old area */
  391.         InvalRect(&GrowRect);                        /* Flag us to update it */
  392.  
  393.         DrawGrowIcon(whichWindow);                    /* Draw the grow Icon again */
  394.         }
  395.     }
  396. return(HandledEvent);
  397. }
  398.  
  399. /* ======================================================= */
  400.  
  401. /* Routine: DoDrag */
  402. /* Purpose: Drag a window around */
  403.  
  404. Boolean CmmApplication::DoDrag(WindowPtr whichWindow,EventRecord *myEvent)
  405. {
  406. Rect    OldRect;                                /* Window rect before the drag */
  407. Rect    tempRect;                                /* temporary rect */
  408. long    theRefCon;                                /* Refcon with layer masked off */
  409. Boolean        HandledEvent;
  410.  
  411.  
  412. HandledEvent = false;
  413. if ((!Doing_MovableModal) || (Doing_MovableModal && (whichWindow == FrontWindow())))/* See if OK to drag */
  414.     {
  415.     OldRect = whichWindow->portRect;            /* Save the rect before resizing */
  416.  
  417.     if (!Doing_MovableModal)                    /* Select proper window */
  418.         Mk_HiliteWindow(whichWindow);
  419.     SetRect(&tempRect, -32000, -32000, 32000, 32000);
  420.     Mk_DragWindow(whichWindow, myEvent->where, tempRect);/* Drag the window */
  421.  
  422.     HandledEvent = true;
  423.     theRefCon = GetWRefCon(whichWindow);        /* Get the refcon */
  424.     theRefCon = theRefCon & 0x00FFFFFF;            /* Mask the layer out */
  425.     switch (theRefCon)                            /* Do the appropiate window */
  426.         {
  427.         case ResD_My_Movable_Moda:
  428.             gMy_Movable_Moda->Moved(&OldRect, whichWindow);    /* Moved this modeless dialog */
  429.             break;
  430.         case ResD_My_Modeless:
  431.             gMy_Modeless->Moved(&OldRect, whichWindow);    /* Moved this modeless dialog */
  432.             break;
  433.         case ResD_About_Demo:
  434.             gAbout_Demo->Moved(&OldRect, whichWindow);    /* Moved this modeless dialog */
  435.             break;
  436.         case ResW_My_basic_window:
  437.             gMy_basic_window->Moved(&OldRect, whichWindow);    /* Moved this window */
  438.             break;
  439.         case ResW_Floating_window:
  440.             gFloating_window->Moved(&OldRect, whichWindow);    /* Moved this window */
  441.             break;
  442.  
  443.         default:                                /* allow other buttons, trap for debug */
  444.             HandledEvent = false;
  445.             break;                                /* end of otherwise */
  446.         }                                        /* end of switch */
  447.     }
  448. return(HandledEvent);
  449. }
  450.  
  451. /* ======================================================= */
  452.  
  453. /* Routine: DoGoAway */
  454. /* Purpose: Close a window */
  455.  
  456. Boolean CmmApplication::DoGoAway(WindowPtr whichWindow,EventRecord *myEvent)
  457. {
  458. long        theRefCon;                            /* Refcon with layer masked off */
  459. Boolean        HandledEvent;
  460.  
  461.  
  462. HandledEvent = false;
  463. if (!Doing_MovableModal)                        /* Select proper window */
  464.     {
  465.     if (TrackGoAway(whichWindow,myEvent->where))    /* See if mouse released in GoAway box */
  466.         {
  467.         HandledEvent = true;
  468.         theRefCon = GetWRefCon(whichWindow);    /* Get the refcon */
  469.         theRefCon = theRefCon & 0x00FFFFFF;        /* Mask the layer out */
  470.         switch (theRefCon)                        /* Do the appropiate window */
  471.             {
  472.             case ResD_My_Movable_Moda:
  473.                 gMy_Movable_Moda->Close(whichWindow);    /* Close this modeless dialog */
  474.                 break;
  475.             case ResD_My_Modeless:
  476.                 gMy_Modeless->Close(whichWindow);    /* Close this modeless dialog */
  477.                 break;
  478.             case ResD_About_Demo:
  479.                 gAbout_Demo->Close(whichWindow);    /* Close this modeless dialog */
  480.                 break;
  481.             case ResW_My_basic_window:
  482.                 gMy_basic_window->Close(whichWindow);    /* Close this window */
  483.                 break;
  484.             case ResW_Floating_window:
  485.                 gFloating_window->Close(whichWindow);    /* Close this window */
  486.                 break;
  487.  
  488.             default:                            /* allow other buttons, trap for debug */
  489.                 HandledEvent = false;
  490.                 break;                            /* end of otherwise */
  491.             }                                    /* end of switch */
  492.         }
  493.     }
  494. return(HandledEvent);
  495. }
  496.  
  497. /* ======================================================= */
  498.  
  499. /* Routine: DoInContent */
  500. /* Purpose: Pressed in the content area */
  501.  
  502. Boolean CmmApplication::DoInContent(WindowPtr whichWindow,EventRecord *myEvent)
  503. {
  504. long        theRefCon;                            /* Refcon with layer masked off */
  505. Boolean        HandledEvent;
  506.  
  507.  
  508. HandledEvent = false;
  509. if (!Doing_MovableModal)                        /* Select proper window */
  510.     {
  511.     if (Mk_Is_FrontWindow(whichWindow) == false)    /* See if already selected or not, in front if selected */
  512.         Mk_HiliteWindow(whichWindow);
  513.     else
  514.         {
  515.         SetPort(whichWindow);                    /* Get ready to draw in this window */
  516.         Mk_HiliteWindow(whichWindow);
  517.         theRefCon = GetWRefCon(whichWindow);    /* Get the refcon */
  518.         theRefCon = theRefCon & 0x00FFFFFF;        /* Mask the layer out */
  519.         HandledEvent = true;
  520.  
  521.         switch (theRefCon)                        /* Do the appropiate window */
  522.             {
  523.             case ResW_My_basic_window:
  524.                 gMy_basic_window->HandleEvent(myEvent);    /* Handle this window */
  525.                 break;
  526.             case ResW_Floating_window:
  527.                 gFloating_window->HandleEvent(myEvent);    /* Handle this window */
  528.                 break;
  529.  
  530.             default:                            /* allow other buttons, trap for debug */
  531.                 HandledEvent = false;
  532.                 break;                            /* end of otherwise */
  533.             }                                    /* end of switch */
  534.         }
  535.     }
  536. return(HandledEvent);
  537. }
  538.  
  539. /* ======================================================= */
  540.  
  541. /* Routine: DoUpdate */
  542. /* Purpose: Got an update event */
  543.  
  544. Boolean CmmApplication::DoUpdate(EventRecord *myEvent)
  545. {
  546. WindowPtr    whichWindow;                        /* See which window for event */
  547. long        theRefCon;                            /* Refcon with layer masked off */
  548. Boolean        HandledEvent;
  549.  
  550.  
  551. whichWindow = (WindowPtr)myEvent->message;        /* Get the window the update is for */
  552. HandledEvent = true;
  553.  
  554. BeginUpdate(whichWindow);                        /* Set the clipping to the update area */
  555. theRefCon = GetWRefCon(whichWindow);            /* Get the refcon */
  556. theRefCon = theRefCon & 0x00FFFFFF;                /* Mask the layer out */
  557. switch (theRefCon)                                /* Do the appropiate window */
  558.     {
  559.     case ResW_My_basic_window:
  560.         gMy_basic_window->Update(whichWindow);        /* Update this window */
  561.         break;
  562.     case ResW_Floating_window:
  563.         gFloating_window->Update(whichWindow);        /* Update this window */
  564.         break;
  565.  
  566.     default:                                    /* allow other buttons, trap for debug */
  567.         HandledEvent = false;
  568.         break;                                    /* end of otherwise */
  569.     }                                            /* end of switch */
  570. EndUpdate(whichWindow);                            /* Return to normal clipping area */
  571. return(HandledEvent);
  572. }
  573.  
  574. /* ======================================================= */
  575.  
  576. /* Routine: CmmApplication.DoActivate */
  577. /* Purpose: Got an activate or deactivate event */
  578.  
  579. Boolean CmmApplication::DoActivate(EventRecord *myEvent)
  580. {
  581. WindowPtr    whichWindow;                        /* See which window for event */
  582. Boolean        Do_An_Activate;                        /* Flag to pass */
  583. Boolean        HandledEvent;
  584. long        theRefCon;                            /* Refcon with layer masked off */
  585.  
  586.  
  587. whichWindow = (WindowPtr)myEvent->message;        /* Get the window the update is for */
  588. HandledEvent = true;
  589.  
  590. Do_An_Activate =  ((myEvent->modifiers & 0x0001) != 0);/* Make sure it is Activate and not DeActivate */
  591. theRefCon = GetWRefCon(whichWindow);            /* Get the refcon */
  592. theRefCon = theRefCon & 0x00FFFFFF;                /* Mask the layer out */
  593. switch (theRefCon)                                /* Do the appropiate window */
  594.     {
  595.     case ResW_My_basic_window:
  596.         gMy_basic_window->Activate(whichWindow,Do_An_Activate);    /* Activate or deactivate this window */
  597.         break;
  598.     case ResW_Floating_window:
  599.         gFloating_window->Activate(whichWindow,Do_An_Activate);    /* Activate or deactivate this window */
  600.         break;
  601.  
  602.     default:                                    /* allow other buttons, trap for debug */
  603.         HandledEvent = false;
  604.         break;                                    /* end of otherwise */
  605.     }                                            /* end of switch */
  606. return(HandledEvent);
  607. }
  608.  
  609. /* ======================================================= */
  610.  
  611. /* Routine: CmmApplication.DoHighLevelEvent */
  612. /* Purpose: Got a High Level event */
  613.  
  614. void CmmApplication::DoHighLevelEvent(EventRecord *myEvent)
  615. {
  616. OSErr    err;                                    /* Error */
  617.  
  618.  
  619. err = AEProcessAppleEvent(myEvent);                /* Go to the processor of apple events */
  620. }
  621.  
  622. /* ======================================================= */
  623.  
  624. /* Routine: CmmApplication.InitExtras */
  625. /* Purpose: Extra Initialization for the application */
  626.  
  627. void CmmApplication::InitExtras()
  628. {
  629. /* Expected to be overridden by the user code */
  630. }
  631.  
  632. /* ======================================================= */
  633.  
  634. /* Routine: CmmApplication.OpenSplashWindow */
  635. /* Purpose: Open spash or personalization window */
  636.  
  637. void CmmApplication::OpenSplashWindow()
  638. {
  639. /* Expected to be overridden by the user code */
  640. }
  641.  
  642. /* ======================================================= */
  643.  
  644. /* Routine: CmmApplication.CloseSplashWindow */
  645. /* Purpose: Close spash or personalization window */
  646.  
  647. void CmmApplication::CloseSplashWindow()
  648. {
  649. /* Expected to be overridden by the user code */
  650. }
  651.  
  652. /* ======================================================= */
  653.  
  654. /* Routine: CmmApplication.Init */
  655. /* Purpose: Initialize the application */
  656.  
  657. void CmmApplication::Init()                        /* Initialize the application */
  658. {
  659.  
  660.  
  661. MoreMasters();                                    /* This reserves space for more handles */
  662. MaxApplZone();                                    /* Give us room for memory allocation */
  663. InitGraf(&qd.thePort);                                /* Quickdraw Init */
  664. InitFonts();                                    /* Font manager init */
  665. InitWindows();                                    /* Window manager init */
  666. InitMenus();                                    /* Menu manager init */
  667. TEInit();                                        /* Text edit init */
  668. InitDialogs(nil);                                /* Dialog manager */
  669.  
  670. FlushEvents(everyEvent,0);                        /* Clear out all events */
  671. InitCursor();                                    /* Make an arrow cursor */
  672.  
  673. doneFlag = FALSE;                                /* Do not exit program yet */
  674.  
  675. gMenus = new CMenus;                            /* Allocate the menu handler object */
  676. gMenus->Init();                                    /* Init the object and the menu bar */
  677.  
  678. gPreferences = new CPreferences;                /* Allocate the preferences object */
  679. gPreferences->Init();                            /* Init the object */
  680.  
  681. gFiles = new CFiles;                            /* Allocate the file handler object */
  682. gFiles->Init();                                    /* Init the object */
  683.  
  684. gPrinting = new CPrinting;                        /* Allocate the printing handler object */
  685. gPrinting->Init();                                /* Init the object */
  686.  
  687. this->OpenSplashWindow();                        /* Open spash or personalization window */
  688.  
  689. theInput = nil;                                    /* Init to no text edit selection active */
  690.  
  691. SleepValue = 40;                                /* Set sleep value */
  692. WNE = this->IsWNEIsImplemented();                /* See if WaitNextEvent is available */
  693.  
  694. UserEventList = nil;                            /* No user events yet */
  695.  
  696. cursorRgn = NewRgn();                            /* Cursor region for WaitNextEvent */
  697.  
  698. gCurrentAlert = nil;
  699. gMy_Alert = new CAMy_Alert;            /* Allocate the alert object */
  700. gMy_Alert->Init();
  701. gMy_Modal = new CDMy_Modal;            /* Allocate the Modal Dialog object */
  702. gMy_Modal->Init();
  703. gMy_Movable_Moda = new CMDMy_Movable_Moda;        /* Allocate the Modeless Dialog object */
  704. gMy_Movable_Moda->Init();
  705. gMy_Modeless = new CMDMy_Modeless;        /* Allocate the Modeless Dialog object */
  706. gMy_Modeless->Init();
  707. gAbout_Demo = new CMDAbout_Demo;        /* Allocate the Modeless Dialog object */
  708. gAbout_Demo->Init();
  709. gMy_basic_window = new CWMy_basic_window;            /* Allocate the window object */
  710. gMy_basic_window->Init();                            /* Initialize Window, My basic window */
  711. gFloating_window = new CWFloating_window;            /* Allocate the window object */
  712. gFloating_window->Init();                            /* Initialize Window, Floating window */
  713.  
  714. Mk_ClearLayers();                                /* Init layer array */
  715. Doing_MovableModal = false;                        /* Not currently doing a movable modal */
  716.  
  717. this->InitExtras();                                /* HOOK, Do extra user initialization */
  718.  
  719. gPreferences->GetPreferences();                    /* Get preferences */
  720.  
  721.  
  722. this->CloseSplashWindow();                        /* Close spash or personalization window */
  723.  
  724. this->OpenOrPrintDroppedOnFiles();                /* Open file user dropped on us to launch us */
  725.  
  726. }
  727.  
  728. /* ======================================================= */
  729.  
  730. /* Routine: CmmApplication.TopOfLoop */
  731. /* Purpose: HOOK at top of main event loop */
  732.  
  733. void CmmApplication::TopOfLoop()
  734. {
  735. /* Expected to be overridden by the user code */
  736. }
  737.  
  738. /* ======================================================= */
  739.  
  740. /* Routine: CmmApplication.FilterEvent */
  741. /* Purpose: HOOK, Allow user to filter all events before the main loop handles them */
  742. /* Set  DoIt  to TRUE to let the main loop handle the event. */
  743.  
  744. void CmmApplication::FilterEvent(Boolean *DoIt, EventRecord *myEvent)
  745. {
  746.  
  747.  
  748. *DoIt = TRUE;                                        /* Let the main loop handle it */
  749. if (myEvent->what == 0)                             /* Handle a NULL event*/
  750.     *DoIt = FALSE;                                    /* ...Tell the main loop to skip it*/
  751. }
  752.  
  753. /* ======================================================= */
  754.  
  755. /* Routine: CmmApplication.Run */
  756. /* Purpose: Run the application */
  757.  
  758. void CmmApplication::Run()                            /* Run the application */
  759. {
  760. Boolean        HandledEvent;
  761.  
  762.  
  763. do
  764.     {
  765.     this->TopOfLoop();                                /* HOOK at top of main event loop */
  766.  
  767.     this->Handle_User_Event();                        /* Check for user events */
  768.  
  769.     if (theInput != NIL)                            /* See if a TE is active */
  770.         TEIdle(theInput);                            /* Blink the cursor if everything is ok */
  771.  
  772.     if (WNE)                                        /* See if do the MultiFinder way */
  773.         DoIt = WaitNextEvent(everyEvent, &myEvent, SleepValue, cursorRgn);/* Wait for an event */
  774.     else
  775.         {
  776.         SystemTask();                                /* For support of desk accessories */
  777.         DoIt = GetNextEvent(everyEvent, &myEvent);    /* See if an event is ready */
  778.         }
  779.  
  780.     this->FilterEvent(&DoIt,&myEvent);                /* HOOK, Let us at the event first */
  781.  
  782.     if (DoIt)                                        /* If event then... */
  783.         {
  784.         Is_A_Dialog = IsDialogEvent(&myEvent);        /* See if a modeless dialog event */
  785.         if (Is_A_Dialog)                            /* Handle a dialog event */
  786.             {
  787.              if (myEvent.what == updateEvt)            /* Handle the update of a Modeless Dialog */
  788.                 {
  789.                 whichWindow = (WindowPtr)myEvent.message; /* Get the window the update is for */
  790.                 BeginUpdate(whichWindow);            /* Set update clipping area */
  791.                 gMy_Movable_Moda->Update(whichWindow);    /* Update Modeless Dialog, My Movable Modal */
  792.                 gMy_Modeless->Update(whichWindow);    /* Update Modeless Dialog, My Modeless */
  793.                 gAbout_Demo->Update(whichWindow);    /* Update Modeless Dialog, About Demo */
  794.                 EndUpdate(whichWindow);                /* Return to normal clipping area */
  795.                 }
  796.             else
  797.                 {
  798.                 DoTheModelessEvent = TRUE;            /* Go ahead and do it so far */
  799.  
  800.                 if (myEvent.what == keyDown)         /* Check the key down, for a command key event */
  801.                     {
  802.                     CmdDown = ((myEvent.modifiers / cmdKey) & 1);/* Get the command key state */
  803.                     charCode = myEvent.message & charCodeMask;/* Get the character */
  804.                     ch = (char)charCode;            /* Change it to ASCII */
  805.  
  806.                     if ((charCode == 13) || (charCode == 0x03))/* CR or Enter */
  807.                         DoTheModelessEvent = TRUE;    /* Handle the default selection */
  808.  
  809.                     if (CmdDown != 0)                /* Handle if the command key was down */
  810.                         {
  811.                         gMenus->EnableTheMenus();    /* Let us in to enable and disable menus*/
  812.                         mResult = MenuKey(ch);        /* See if a menu selection */
  813.                         theMenu = HiWord(mResult);    /* Get the menu list number */
  814.                         theItem = LoWord(mResult);    /* Get the menu item number */
  815.                         if (theMenu != 0)              /* See if a list was selected */
  816.                             gMenus->HandleMenuSelection(theMenu,theItem);    /* Do the menu selection */
  817.  
  818.                         whichWindow = FrontWindow(); /* Get the front window */
  819.                         if ((ch == 'x') || (ch == 'X'))    /* Handle a CUT */
  820.                             DlgCut(whichWindow);
  821.                         if ((ch == 'c') || (ch == 'C'))    /* Handle a COPY */
  822.                             DlgCopy(whichWindow);
  823.                         if ((ch == 'v') || (ch == 'V'))    /* Handle a PASTE */
  824.                             DlgPaste(whichWindow);
  825.  
  826.                         DoTheModelessEvent = FALSE;/* We handled the command key */
  827.                         }
  828.                     }
  829.  
  830.                 if (DoTheModelessEvent)             /* Do we handle it? */
  831.                     {
  832.                     if ((DialogSelect(&myEvent, &whichWindow, &itemHit)) || (myEvent.what == mouseDown) || (myEvent.what == keyDown)) /* Ck if do it */
  833.                         {
  834.                         gMy_Movable_Moda->HandleEvent(&myEvent,whichWindow,itemHit);    /* Handle the Modeless Dialog, My Movable Modal */
  835.                         gMy_Modeless->HandleEvent(&myEvent,whichWindow,itemHit);    /* Handle the Modeless Dialog, My Modeless */
  836.                         gAbout_Demo->HandleEvent(&myEvent,whichWindow,itemHit);    /* Handle the Modeless Dialog, About Demo */
  837.                         }
  838.                     }
  839.                 }
  840.             }
  841.         else
  842.             {
  843.  
  844.             switch (myEvent.what)                    /* Decide type of event */
  845.                 {
  846.                 case mouseDown :                /* Mouse button pressed */
  847.                     code = FindWindow(myEvent.where, &whichWindow);/* Get which window the event happened in */
  848.  
  849.                     switch (code)                /* Decide type of event again */
  850.                         {
  851.                         case inMenuBar :        /* In the menubar */
  852.                             gMenus->EnableTheMenus();            /* Let us in to enable and disable menus*/
  853.                             mResult = MenuSelect(myEvent.where);/* Do menu selection */
  854.                             theMenu = HiWord(mResult);            /* Get the menu list number */
  855.                             theItem = LoWord(mResult);            /* Get the menu list item number */
  856.                             gMenus->HandleMenuSelection(theMenu,theItem);    /* Do the menu selection */
  857.                             break;
  858.  
  859.                         case inDrag :        /* In window drag area */
  860.                             HandledEvent = this->DoDrag(whichWindow,&myEvent);
  861.                             break;
  862.  
  863.                         case inGrow :        /* In window grow area */
  864.                             HandledEvent = this->DoGrow(whichWindow,&myEvent);
  865.                             break;
  866.  
  867.                         case inZoomIn :        /* In window zoom area */
  868.                         case inZoomOut :
  869.                             HandledEvent = this->DoZoom(whichWindow,code,&myEvent);
  870.                             break;
  871.  
  872.                         case inGoAway :        /* In window goaway area */
  873.                             HandledEvent = this->DoGoAway(whichWindow,&myEvent);
  874.                             break;
  875.  
  876.                         case inContent :    /* In window  contents */
  877.                             HandledEvent = this->DoInContent(whichWindow,&myEvent);
  878.                             break;
  879.  
  880.                         case inSysWindow :    /* See if a DA selection */
  881.                             SystemClick(&myEvent, whichWindow);    /* Let other programs in */
  882.                             break;
  883.  
  884.                         default:            /* Handle others */
  885.                             break;            /* End of otherwise */
  886.                         }                    /* End of code case */
  887.                     break;                    /* End of MouseDown */
  888.  
  889.                 case keyDown:                /* Handle key inputs */
  890.                 case autoKey:                /* and auto repeats */
  891.                     this->DoKeyEvent(&myEvent);
  892.                     break;
  893.  
  894.                 case updateEvt :            /* Update event for a window */
  895.                     HandledEvent = this->DoUpdate(&myEvent);
  896.                     break;
  897.  
  898.                 case diskEvt :                /* Disk inserted event */
  899.                     this->DoDiskEvent(&myEvent);
  900.                     break;
  901.  
  902.                 case activateEvt :            /* Window activated event */
  903.                     HandledEvent = this->DoActivate(&myEvent);
  904.                     break;
  905.  
  906.                 case osEvt:                    /* OS event */
  907.                     this->DoOSEvent(&myEvent);
  908.                     break;
  909.  
  910.                 case kHighLevelEvent:        /* High Level event */
  911.                     if (Has.AppleEvents)    /* See if has Apple Events */
  912.                         this->DoHighLevelEvent(&myEvent);    /* Do AppleEvents */
  913.                     break;
  914.  
  915.                 default:                    /* Used for debugging, to see what other events are coming in */
  916.                     break;                    /* End of otherwise */
  917.  
  918.                 }                            /* End of case */
  919.  
  920.             }
  921.         }
  922.     else
  923.         {
  924.         whichWindow = FrontWindow();        /* Get the current front window */
  925.         if (whichWindow != NIL)                /* See if we have a window */
  926.             {
  927.             thePeeked = (WindowPeek)whichWindow;        /* Peek inside, look for dialog */
  928.             if (thePeeked->windowKind == dialogKind)    /* DialogSelect will crash if no dialogs */
  929.                 {
  930.                 if (DialogSelect(&myEvent, &whichWindow, &itemHit))/* Blink cursor in modeless TEs */
  931.                     {
  932.                     }
  933.                 }
  934.             }
  935.         }
  936.     }                                        /* end of while */
  937. while (!doneFlag);                            /* End of the event loop */
  938. }
  939.  
  940. /* ======================================================= */
  941.  
  942. /* Routine: CmmApplication.ExitExtras */
  943. /* Purpose: Exit the application */
  944.  
  945. void CmmApplication::ExitExtras()                            /* Exit the application */
  946. {
  947. /* Expected to be overridden by the user code */
  948. }
  949.  
  950. /* ======================================================= */
  951.  
  952. /* Routine: CmmApplication.Exit */
  953. /* Purpose: Exit the application */
  954.  
  955. void CmmApplication::Exit()                            /* Exit the application */
  956. {
  957.  
  958.  
  959. gMy_Movable_Moda->Close(gMy_Movable_Moda->theWindow);    /* Close this modeless dialog */
  960. gMy_Modeless->Close(gMy_Modeless->theWindow);    /* Close this modeless dialog */
  961. gAbout_Demo->Close(gAbout_Demo->theWindow);    /* Close this modeless dialog */
  962. gMy_basic_window->Close((WindowPtr)-1);    /* Close this window */
  963. gFloating_window->Close((WindowPtr)-1);    /* Close this window */
  964.  
  965. gPreferences->SetPreferences();                    /* Set default preferences */
  966.  
  967. delete gMy_Alert;                        /* Deallocate the alert object */
  968. delete gMy_Modal;                        /* Deallocate the Modal object */
  969. delete gMy_Movable_Moda;                        /* Deallocate the Modeless object */
  970. delete gMy_Modeless;                        /* Deallocate the Modeless object */
  971. delete gAbout_Demo;                        /* Deallocate the Modeless object */
  972. delete gMy_basic_window;                        /* Deallocate the window object */
  973. delete gFloating_window;                        /* Deallocate the window object */
  974. ExitExtras();                                        /* HOOK, Do extra user exit code */
  975. }
  976.  
  977. /* ======================================================= */
  978. /* ======================================================= */
  979.  
  980.  void main()                                    /* Start of main body */
  981. {
  982.  
  983.  
  984. gApplication = new CApplication;                /* Allocate the application object */
  985.  
  986. gApplication->Init();                            /* Init the object and the application */
  987.  
  988. gApplication->Run();                            /* Run the application */
  989.  
  990. gApplication->Exit();                            /* Exit the application */
  991.  
  992. }                                                /* end of main */
  993.